package com.atguigu.yygh.cmn.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.yygh.cmn.cfg.listener.DictDataListener;
import com.atguigu.yygh.cmn.mapper.DictMapper;
import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.hosp.model.cmn.Dict;
import com.atguigu.yygh.hosp.vo.cmn.DictEeVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @version 1.0
 * @Author: 许啸林
 * @Date: 2021/7/7 15:32
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    /**
     * 获取当前节点的子节点
     * @param id
     * @return
     */
    @Override
    public List<Dict> getDictItem(Long id) {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(Dict::getParentId, id);
        List<Dict> childDicts = baseMapper.selectList(queryWrapper);
        if(!StringUtils.isEmpty(childDicts)) {
            childDicts = childDicts.stream().map(dictItem -> {
               dictItem.setHasChildren(hasChild(dictItem.getId()));
               return dictItem;
            }).collect(Collectors.toList());
        }
        return childDicts;
    }

    /**
     * 导出数据字典
     * @param response
     */
    @Override
    public void exprotData(HttpServletResponse response) {
        List<Dict> dicts = baseMapper.selectList(null);
        List<DictEeVo> dictVoList = dicts.stream().map(item -> {
            DictEeVo dictEeVo = new DictEeVo();
            BeanUtils.copyProperties(item, dictEeVo);
            return dictEeVo;
        }).collect(Collectors.toList());
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("数据字典", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), DictEeVo.class).sheet("数据字典").doWrite(dictVoList);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }

    }

    /**
     * 导入数据字典
     * @param file
     */
    @Override
    public void importDict(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), DictEeVo.class, new DictDataListener(this)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
    }

    /**
     * 判断数据字典是否含有子节点
     * @param id
     * @return
     */
    public Boolean hasChild(Long id) {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(Dict::getParentId, id);
        Integer count = baseMapper.selectCount(queryWrapper);
        return  count > 0;
    }

}
